package cn.shyouchi.wuguanjia.android.ui.event.checkpoint;

import android.content.Context;
import android.support.annotation.NonNull;

import java.util.List;

import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.event.post.EventPostContract;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Eenie on 2016/11/17 at 16:31
 * Email: 472279981@qq.com
 * Des:
 */

public class CheckPointPresenter implements CheckPointContract.Presenter {

    Context mContext;
    CheckPointContract.View mView;
    EventApi mEventApi;

    public CheckPointPresenter(Context context, CheckPointContract.View view) {

        this.mContext = context;
        this.mView = view;
        mEventApi = new EventApi(context);
    }

    @Override
    public void getPointList(String projectId) {
        mEventApi.getPointList(projectId)
                .subscribeOn(Schedulers.io())
                .map(new Func1<CheckPointEntity, List<CheckPointEntity.ResultMessageBean>>() {
                    @Override
                    public List<CheckPointEntity.ResultMessageBean> call(CheckPointEntity checkPointEntity) {
                        return checkPointEntity.getResultMessage();
                    }

                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<CheckPointEntity.ResultMessageBean>>(mContext) {

                    @Override
                    public void onNext(List<CheckPointEntity.ResultMessageBean> resultMessageBeanList) {
                        mView.onDataRefresh(resultMessageBeanList);
                    }
                });
    }

    @Override
    public void delPoint(final String projectId, String id) {
        mEventApi.delPoint(id).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult result) {
                        getPointList(projectId);
                    }
                });
    }

    public void sortPoint(final String projectId, List<CheckPointEntity.ResultMessageBean> ids) {
        mEventApi.sortPoint(projectId, ids).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult result) {
                        getPointList(projectId);
                    }
                });
    }

    @Override
    public void attachView(@NonNull EventPostContract.View view) {

    }

    @Override
    public void detachView() {

    }
}
